﻿using Common;
using Core;
using DAL.Models;
using EntityFramework.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.DAO
{
    public class ${TableName}DAO
    {

        /// <summary>
        /// 根据序列号sn 获取对应的睡眠检测仪设备
        /// </summary>
        /// <param name="serialNumber">序列号</param>
        /// <returns></returns>
        public ${TableName} GetDeviceBySN(string sn)
        {
            using (hardware_platformContext db = new hardware_platformContext())
            {
                return db.${TableName}.AsNoTracking().Where(x => x.imei == sn).FirstOrDefault();
            }
        }


        /// <summary>
        /// 获取${table.comment}
        /// </summary>
        /// <param name="serial_number"></param>
        /// <param name="pin"></param>
        /// <param name="user_name"></param>
        /// <param name="user_idcard"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="total"></param>
        /// <param name="records"></param>
        /// <returns></returns>
        public IEnumerable<dynamic> GetDeviceFilter(string sn, string user_name, int page, int rows, ref int total, ref int records)
        {
            using (hardware_platformContext db = new hardware_platformContext())
            {
                var queryTemp = from tb1 in db.${TableName}
                                join tb2 in db.device_user_bind on new { dev_id = tb1.id, dev_type = Codebooks.CODE_${TableNameUpper} } equals new { dev_id = tb2.device_id.Value, dev_type = tb2.device_type.Value }
                                into tb3
                                from tb4 in tb3.DefaultIfEmpty()
                                select new
                                {
                                    device_id = tb1.id,
                                    tb1.imei,
                                    tb1.submit_time,
                                    tb1.comment,
                                    user_id = tb4.user_id,
                                    tb1.create_date,
                                    tb1.model,
                                    relation_id = tb4.id,

                                };
                var query = from tb1 in queryTemp
                            join tb2 in db.user_info on tb1.user_id equals tb2.id into tb3
                            from tb4 in tb3.DefaultIfEmpty()
                            select new
                            {
                                device_id = tb1.device_id,
                                tb1.imei,
                                tb1.comment,
                                tb4.phone,
                                user_id = tb1.user_id,
                                user_name= tb4.name,
                                tb1.create_date,
                                tb1.submit_time,
                                tb1.model,
                                relation_id = tb1.relation_id == null ? 0 : tb1.relation_id,
                                id=tb1.device_id
                            };

                if (!string.IsNullOrEmpty(sn))
                    query = query.Where(x => x.imei.Contains(sn));

                if (!string.IsNullOrEmpty(user_name))
                    query = query.Where(x => x.user_name.Contains(user_name));

                records = query.Count();
                total = (int)Math.Ceiling(Convert.ToDouble(records) / rows);
                query = query.OrderByDescending(x =>x.create_date ).Skip((page - 1) * rows).Take(rows);

                return query.ToList();
            }
        }




        /// <summary>
        /// 获取${table.comment}
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ${TableName} GetById(long id)
        {
            using (hardware_platformContext db = new hardware_platformContext())
            {
                return db.${TableName}.AsNoTracking().Where(x => x.id == id).FirstOrDefault();
            }
        }




        /// <summary>
        /// 修改${table.comment}信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ArgsHelper Update(${TableName} model)
        {
            using (hardware_platformContext db = new hardware_platformContext())
            {
                try
                {
                    var result = db.${TableName}.Where(x => x.id == model.id).FirstOrDefault();

                    result.imei = model.imei;
                    result.comment = model.comment;
                    result.updater = model.updater;
                    result.update_date = model.update_date;  
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    return new ArgsHelper(ex.Message);
                }
                return new ArgsHelper(true, "修改成功");
            }
        }



        /// <summary>
        /// 删除${table.comment}
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ArgsHelper DeleteById(long id)
        {
            using (hardware_platformContext db = new hardware_platformContext())
            {
                try
                {
                    db.${TableName}.AsNoTracking().Where(x => x.id == id).Delete();
                }
                catch (Exception ex)
                {

                    return new ArgsHelper(ex.Message);
                }
                return new ArgsHelper(true, "成功");
            }
        }


        /// <summary>
        /// 获取${table.comment}
        /// </summary>
        /// <param name="imei"></param>
        /// <returns></returns>
        public ${TableName} GetByImei(string sn)
        {
            using (hardware_platformContext db = new hardware_platformContext())
            {
                return db.${TableName}.AsNoTracking().Where(x => x.imei == sn).FirstOrDefault();
            }
        }


        /// <summary>
        /// 添加${table.comment}
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ArgsHelper Add(${TableName} model)
        {
            using (hardware_platformContext db = new hardware_platformContext())
            {
                try
                {
                    db.${TableName}.Add(model);
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    return new ArgsHelper(ex.Message);
                }
                return new ArgsHelper(true, (Object)model.id.ToString());
            }
        }


        /// <summary>
        /// 获取某用户绑定的所有可用的${table.comment}
        /// </summary>
        /// <param name="user_name">用户名</param>
        /// <param name="imei"></param>
        /// <param name="phone">手表电话</param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="total"></param>
        /// <param name="record"></param>
        /// <returns></returns>
        public IEnumerable<${TableName}> GetByUser(long user_id, int page, int rows, ref int total, ref int records)
        {
            using (hardware_platformContext db = new hardware_platformContext())
            {
                var query = from tb1 in db.${TableName}
                            from tb2 in db.device_user_bind
                            where tb1.id == tb2.device_id && tb2.device_type == Codebooks.CODE_${TableNameUpper}
                                && tb2.user_id == user_id

                            select tb1;

                records = query.Count();
                total = (int)Math.Ceiling(Convert.ToDouble(records) / rows);
                query = query.OrderByDescending(x => x.id).Skip((page - 1) * rows).Take(rows);

                return query.ToList();
            }
        }



        /// <summary>
        /// 获取设备的所有sn号码
        /// </summary>
        /// <returns></returns>
        public List<string> List${ClassName}Sn() {
            using (hardware_platformContext db = new hardware_platformContext()) {

                var query = from tb1 in db.${TableName}
                            from tb2 in db.device_user_bind
                            where tb1.id == tb2.device_id && tb2.device_type == Codebooks.CODE_${TableNameUpper}
                            select tb1;

                return query.Select(x=>x.imei).Distinct().ToList();
            }

        }



        /// <summary>
        /// 修改${table.comment}最新上传数据时间
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ArgsHelper UpdateSubmitTime(${TableName} model)
        {
            using (hardware_platformContext db = new hardware_platformContext())
            {
                try
                {
                    var result = db.${TableName}.Where(x => x.id == model.id).FirstOrDefault();

                    result.submit_time = DateTime.Now;
                    result.updater = -1;
                    result.update_date = DateTime.Now;
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    return new ArgsHelper(ex.Message);
                }
                return new ArgsHelper(true, "修改成功");
            }
        }

    }
}
